package com.shujia.jdbc;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

//import org.apache.phoenix.jdbc.PhoenixDriver;
public class PhoenixJDBC {
    public static void main(String[] args) throws Exception{
        String driver = "org.apache.phoenix.jdbc.PhoenixDriver";
        String url = "jdbc:phoenix:master,node1,node2:2181";

        //加载驱动，通过反射的方式加载
        Class.forName(driver);

        //创建与phoenix的连接对象
        Connection conn = DriverManager.getConnection(url);

        PreparedStatement preparedStatement = conn.prepareStatement("select /*+ INDEX(DIANXIN DIANXIN_INDEX_COVER) */ * from DIANXIN where x=? and y =?");
        preparedStatement.setDouble(1, 81.518);
        preparedStatement.setDouble(2, 43.978);

        //执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            /*
             mdn VARCHAR ,
             start_date VARCHAR ,
             end_date VARCHAR ,
             county VARCHAR,
             x DOUBLE ,
             y  DOUBLE,
             bsid VARCHAR,
             grid_id  VARCHAR,
             biz_type VARCHAR,
             event_type VARCHAR ,
             data_source VARCHAR ,
             */
            String mdn = resultSet.getString("mdn");
            String start_date = resultSet.getString("start_date");
            String end_date = resultSet.getString("end_date");
            String county = resultSet.getString("county");
            double x = resultSet.getDouble("x");
            double y = resultSet.getDouble("y");
            String bsid = resultSet.getString("bsid");
            String grid_id = resultSet.getString("grid_id");
            String biz_type = resultSet.getString("biz_type");
            String event_type = resultSet.getString("event_type");
            String data_source = resultSet.getString("data_source");

            System.out.println(mdn+","+start_date+","+end_date+","+county+","+x+","+y+","+bsid+","+grid_id+","+biz_type+","+event_type+","+data_source);

        }

        //关闭连接释放资源
        preparedStatement.close();
        conn.close();

    }
}
